﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;

namespace UdrinkWater_Project.Customer
{
    public partial class OrderReport : System.Web.UI.Page
    {
        private string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            string OrderID = Session["Date"].ToString() +" "+ Session["Time"].ToString();
            string list = Session["list"].ToString();
            string SumAll = Session["SumAll"].ToString();
            string Amphur = Session["Distric"].ToString();
            string Distric = Session["Amphur"].ToString();
            string Province = Session["Province"].ToString();
            string Zip = Session["Zipcode"].ToString();
            //string a = Session["DocDate"].ToString();
            //DateTime docDate = Convert.ToDateTime(a);
            
            //string DocDate = ConvestToDateN((docDate.Date).ToShortDateString());
            //string b = Session["DeeadLine"].ToString();
            //DateTime DeadLine = Convert.ToDateTime(a);
            //string DeeadLine = ConvestToDateN((DeadLine.Date).ToShortDateString());
            //string PrintDate = ConvestToDateN((DateTime.Now).ToShortDateString());


            SqlConnection objConn = new SqlConnection();
            SqlCommand objCmd = new SqlCommand();
            SqlDataAdapter dtAdapter = new SqlDataAdapter();

            DataSet ds = new DataSet();
            DataTable dt = null;
            string strConnString = null;
            StringBuilder strSQL = new StringBuilder();
            


            strSQL.Append(" SELECT        TB_Order.Order_Date, TB_Order.Customer_ID, (TB_Customer.Prefix + ' '+ TB_Customer.Firstname+' '+ TB_Customer.Lastname)as CustomerName, TB_Order.Delivery_Address, TB_Orderlist.Product_ID, TB_Product.Product_name,  ");
            strSQL.Append(" TB_Product.Unit, TB_Unit_Category.Unit_Name, TB_Orderlist.Product_Count, TB_Orderlist.Price, TB_Orderlist.Discount,((TB_Orderlist.Product_Count * TB_Orderlist.Price) - TB_Orderlist.Discount) AS Total , ROW_NUMBER() OVER(ORDER BY TB_Orderlist.Product_ID) ROW ");
            strSQL.Append(" FROM            TB_Order LEFT OUTER JOIN ");
            strSQL.Append(" TB_Customer ON TB_Order.Customer_ID = TB_Customer.ID_Card LEFT OUTER JOIN ");
            strSQL.Append(" TB_Orderlist ON TB_Order.Order_Date = TB_Orderlist.Order_Date LEFT OUTER JOIN ");
            strSQL.Append(" TB_Product ON TB_Orderlist.Product_ID = TB_Product.Product_ID LEFT OUTER JOIN ");
            strSQL.Append("  TB_Unit_Category ON TB_Product.Unit = TB_Unit_Category.Unit_ID");
            strSQL.Append(" WHERE TB_Order.Order_Date = '" + OrderID + "'");
          

            objConn.ConnectionString = Connection;
            var _with1 = objCmd;
            _with1.Connection = objConn;
            _with1.CommandText = strSQL.ToString();
            _with1.CommandType = CommandType.Text;
            dtAdapter.SelectCommand = objCmd;

            dtAdapter.Fill(ds, "CustomerReportOrder");
            dt = ds.Tables[0];

            dtAdapter = null;
            objConn.Close();
            objConn = null;

            ReportDocument rpt = new ReportDocument();
            rpt.Load(Server.MapPath("CustomerReportOrder.rpt"));

            rpt.SetDataSource(dt);


            rpt.SetParameterValue("list", list);
            rpt.SetParameterValue("SumAll", SumAll);
            rpt.SetParameterValue("Amphur", Amphur);
            rpt.SetParameterValue("Distric", Distric);
            rpt.SetParameterValue("Province", Province);
            rpt.SetParameterValue("Zip", Zip);
            //this.OrderReportView.ReportSource = rpt;
            rpt.ExportToHttpResponse
            (CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, true, "CustomerReceipt");
        }
    }
}